package com.activemq.demo.activemq_service.controller;

import com.activemq.demo.activemq_service.service.MessageProcessingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/management")
public class ManagementController {

    @Autowired
    private MessageProcessingService messageProcessingService;

    @PostMapping("/reset-counters")
    public ResponseEntity<Map<String, Object>> resetCounters() {
        messageProcessingService.resetCounters();

        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "Counters have been reset");

        return ResponseEntity.ok(response);
    }

    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getSystemStatus() {
        Map<String, Object> status = new HashMap<>();

        // 系统状态信息
        status.put("system", "Active");
        status.put("timestamp", System.currentTimeMillis());

        // 消息处理状态
        Map<String, Object> messageStatus = new HashMap<>();
        messageStatus.put("processed", messageProcessingService.getProcessedCount());
        messageStatus.put("failed", messageProcessingService.getFailedCount());
        messageStatus.put("deadLetter", messageProcessingService.getDeadLetterCount());

        status.put("messageProcessing", messageStatus);

        // 队列状态（这里可以添加实际的队列状态信息）
        Map<String, Object> queueStatus = new HashMap<>();
        queueStatus.put("mainQueue", "Active");
        queueStatus.put("deadLetterQueue", "Active");

        status.put("queues", queueStatus);

        return ResponseEntity.ok(status);
    }
}